﻿#include "systemsetitem.h"

#include <QDebug>
#include <QGraphicsScene>

SystemSetItem::SystemSetItem(const QStringList& valueList, QObject *parent, QGraphicsItem *item) : QObject(parent), QGraphicsItem(item)
{
    //缓存在设备坐标系中，并且只有在位置、大小或外观发生变化时才需要重新计算。
    this->setCacheMode(QGraphicsItem::DeviceCoordinateCache);

    // 标题
    m_title = valueList.at(0);
    // 阶段标志
    m_stage = valueList.at(1);
    // 温度, 湿度
    m_tempeture = valueList.at(2);
    m_humidity = valueList.at(3);
    // 测试人员, 测试日期
    m_person = valueList.at(4);
    m_date = valueList.at(5);
    // 伺服机构编号, 控制器编号
    m_a1Num = valueList.at(6);
    m_a2Num = valueList.at(7);
    m_a3Num = valueList.at(8);
    m_a4Num = valueList.at(9);
    m_ctrlNum = valueList.at(10);
    // 设备编号, 软件版本号
    m_deviceId = valueList.at(11);
    m_version = valueList.at(12);
    // 测试说明
    m_description = valueList.at(13);
}

QRectF SystemSetItem::boundingRect() const
{
    return QRectF(0, 0, scene()->width(), 360);
}

void SystemSetItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
    painter->setCompositionMode(QPainter::CompositionMode_Source);

    QPen pen;
    pen.setColor(Qt::black);
    painter->setPen(pen);
    painter->setFont(QFont("Helvetica", 12));

    // 绘制网格线
    for (int i = 0; i < 10; i++)
        painter->drawLine(QLineF(30, i * 40, 870, i * 40));

    painter->drawLine(QLineF(30, 0, 30, 360));
    painter->drawLine(QLineF(160, 40, 160, 360));
    painter->drawLine(QLineF(280, 40, 280, 120));
    painter->drawLine(QLineF(410, 40, 410, 200));
    painter->drawLine(QLineF(560, 40, 560, 80));
    painter->drawLine(QLineF(720, 40, 720, 80));
    painter->drawLine(QLineF(650, 120, 650, 200));
    painter->drawLine(QLineF(870, 0, 870, 360));

    // 文字说明
    painter->drawText(QPointF(40, 65), QStringLiteral("阶段标志"));
    painter->drawText(QPointF(290, 65), QStringLiteral("环境温度(℃)"));
    painter->drawText(QPointF(590, 65), QStringLiteral("环境湿度(%)"));
    painter->drawText(QPointF(40, 105), QStringLiteral("测试人员"));
    painter->drawText(QPointF(300, 105), QStringLiteral("测试日期"));
    painter->drawText(QPointF(40, 145), QStringLiteral("A1伺服机构编号"));
    painter->drawText(QPointF(440, 145), QStringLiteral("A2伺服机构编号"));
    painter->drawText(QPointF(40, 185), QStringLiteral("A3伺服机构编号"));
    painter->drawText(QPointF(440, 185), QStringLiteral("A4伺服机构编号"));
    painter->drawText(QPointF(40, 225), QStringLiteral("控制器编号"));
    painter->drawText(QPointF(40, 265), QStringLiteral("测试设备编号"));
    painter->drawText(QPointF(40, 305), QStringLiteral("软件版本号"));
    painter->drawText(QPointF(40, 345), QStringLiteral("测试说明"));

    // 文字内容
    painter->drawText(QPointF(320, 25), m_title);
    painter->drawText(QPointF(200, 65), m_stage);
    painter->drawText(QPointF(450, 65), m_tempeture);
    painter->drawText(QPointF(760, 65), m_humidity);
    painter->drawText(QPointF(200, 105), m_person);
    painter->drawText(QPointF(460, 105), m_date);
    painter->drawText(QPointF(200, 145), m_a1Num);
    painter->drawText(QPointF(680, 145), m_a2Num);
    painter->drawText(QPointF(200, 185), m_a3Num);
    painter->drawText(QPointF(680, 185), m_a4Num);
    painter->drawText(QPointF(200, 225), m_ctrlNum);
    painter->drawText(QPointF(200, 265), m_deviceId);
    painter->drawText(QPointF(200, 305), m_version);
    painter->drawText(QPointF(200, 345), m_description);

}
